<template>
  <div id="subscriber">
    <div class="register-box">
      <h3 class="name">注册账号</h3>
      <el-form
        :model="registerForm"
        status-icon
        :rules="rules"
        ref="registerForm"
        label-width="60px"
        class="demo-registerForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            type="text"
            v-model="registerForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="registerForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('registerForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('registerForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var validateUser = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入注册账号"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入注册密码"));
      } else {
        callback();
      }
    };
    return {
      registerForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { validator: validateUser, trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { validator: validatePass, trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let { username, password } = this.registerForm;
          this.$api
            .getRegister({
              username,
              password,
            })
            .then((res) => {
              // console.log(res.data);
              if (res.data.status === 200) {
                this.$message({
                  message: "恭喜你注册成功！！！！",
                  type: "success",
                });
                this.$router.push("/login");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.name {
  text-align: center;
  margin-bottom: 30px;
  font-size: 20px;
}
.register-box {
  width: 400px;
  height: 300px;
  padding: 20px;
  margin: 100px auto;
  margin-top: 190px;
  background-color: rgba(255, 255, 255, 0.5);
}
#subscriber {
  background: url("../../assets/images/2.jpg");
  width: 100%;
  height: 100%;
  position: fixed;
  background-size: 100% 100%;
}
</style>